import add from './admin-add.js';//模块静态导入
import update from './admin-update.js';
import del from './admin-Del.js';

let datagrid = document.querySelector(".datagrid");
let loadData = function(){
    fetch("admin-list.let").then((r)=>r.json()).then((data)=>{
        console.debug(data);
        datagrid.innerHTML = "";
        data.forEach((item)=>{
            let tr = document.createElement("tr");
            tr.innerHTML=` 
            <td>${item.adminId}</td>
            <td>${item.adminName}</td>
            <td>${item.adminTime}</td>
            <td style="width: 10em;">
                <div class="btn-group ml-2">
                    <button type="button" class="btn btn-primary _update">修改</button>
                    <button type="button" class="btn btn-danger _delete">删除</button>
                </div>
            </td>`;
            datagrid.appendChild(tr);
            tr.querySelector("._update").onclick = function (){
                update(item);
            }
            tr.querySelector("._delete").onclick = function (){
                del(item);
            }
        });
    }).catch((e)=>{
        console.error(e);
    })
}
loadData();
window.addEventListener("message",function(e){
    console.debug(e);
    if(e.data == "loadData"){
        loadData();
    }
})
add();